#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BasicFileReader.h"

char * BasicFileReader::GetNextTag(FILE * fptr)
{
	//char * buf = (char *)malloc(sizeof(char) * 
	char c;
	fpos_t startPtr; 
	int startIndex = 0;
	int endIndex = 0;
	int count = 0;
	bool started = false;
	bool ended = false;
	while(fread(&c, sizeof(char), 1, fptr) == 1)
	{
		if(c == '<')
		{
			fgetpos(fptr, &startPtr);
			startIndex = count;
			started = true;
		}
		if(c == '>' && started)
		{
			endIndex = count;
			ended = true;
			break;
		}
		count ++;
	}
	
	if(!started || !ended)
	{
		return NULL;
	}

	fsetpos(fptr, &startPtr);
	fseek(fptr,-1, SEEK_CUR);
	int stringSize = endIndex - startIndex + 1;
	char * buf = (char *) malloc(sizeof(char) * stringSize + 1);
	fread(buf, sizeof(char),stringSize, fptr);
	buf[stringSize] = '\0';
	return buf;
}		

char * BasicFileReader::GetNextElement(char * tag, int * pos)
{
	if(tag == NULL)
		return NULL;
		
	int startIndex = *pos;
	if(tag[startIndex] == '\0')
		return NULL;
	int endIndex = 0;
	int openparen = 0;
	bool openQuote = false;
	int stringSize = 0;
	int stringLen = strlen(tag);
	bool closeElement = false;
	for(int i = startIndex; i < stringLen; i++)
	{
		
		if(tag[i] == '<')
			startIndex = i+1;
		if(tag[i] == '(')
			openparen++;
		if(tag[i] == ')' && openparen > 0)
			openparen--;
		if(tag[i] == '"')
		{
			openQuote = !openQuote;
			printf("openQuote = %d\n", openQuote);
		}
			
		if(!openQuote && openparen == 0 && (tag[i] == ',' || tag[i] == '>'))
		{
			endIndex = i - 1;
			*pos = i+1;
			closeElement = true;
			break;
		}
		
		if(tag[i] == '\0')
			return NULL;
		if(openQuote || (tag[i] != '<' && tag[i] != ' ' && tag[i] != '\t' && tag[i] != '\n'))
		{
			stringSize ++;
		}
		if(tag[i+1]=='\0')
		{
			endIndex = i;
			*pos = i+1;
			closeElement = true;
			break;
		}
	}
	if(!closeElement)
		return NULL;
	char * buf = (char *) malloc(sizeof(char) * stringSize+1);
	int count = 0;
	openQuote = false;
	for(int i = startIndex; i <= endIndex && count < stringSize; i++)
	{
		
		if(tag[i] == '"')
			openQuote = !openQuote;
		
		if(openQuote || (tag[i] != ' ' && tag[i] != '\t' && tag[i] != '\n'))
		{
			buf[count] = tag[i];
			count++;
		}
	}
	buf[stringSize] = '\0';
	return buf;
}
Vector2D BasicFileReader::ReadCoordinate(char * element)
{
	Vector2D coord;
	if(sscanf(element, "(%f,%f)", &(coord.x), &(coord.y))!= 2)
	{
		printf("Error reading coordinate %s\n", element);
	}
	return coord;
}
float BasicFileReader::ReadFloat(char * element)
{
	float num;
	if(sscanf(element, "%f", &num)!= 1)
	{
		printf("Error reading float %s\n", element);
	}
	return num;
}
int BasicFileReader::ReadInt(char * element)
{
	int num;
	if(sscanf(element, "%d", &num)!= 1)
	{
		printf("Error reading integer %s\n", element);
	}
	return num;
}
Vector2D BasicFileReader::ReadNextCoordinate(char * tag, int * pos)
{
	char * element = GetNextElement(tag, pos);
	Vector2D c = ReadCoordinate(element);
	if(element != NULL)
		free(element);
	element = NULL;
	return c;
}
int BasicFileReader::ReadNextInt(char * tag, int * pos)
{
	char * element = GetNextElement(tag, pos);
	int i = ReadInt(element);
	if(element != NULL)
		free(element);
	element = NULL;
	return i;
}
float BasicFileReader::ReadNextFloat(char * tag, int * pos)
{
	char * element = GetNextElement(tag, pos);
	float f = ReadFloat(element);
	if(element != NULL)
		free(element);
	element = NULL;
	return f;
}
char * BasicFileReader::ReadNextString( char * tag, int * pos)
{
	char * element = GetNextElement(tag, pos);
	return element;
}
Vector2D * BasicFileReader::ReadNextVectorArray(int numVertices, char * tag, int * pos)
{
	Vector2D * vertices = (Vector2D *)malloc(sizeof(Vector2D) * numVertices);
	for(int i = 0; i < numVertices; i++)
	{
		vertices[i] = ReadNextCoordinate(tag, pos);
	}
	return vertices;
}

